<template>
  <a-modal v-model="visible" :title="title" >
    <template slot="footer">
      <a-button key="back" @click="handleCancel">
        返回
      </a-button>
      <a-button key="submit" type="primary" :loading="loading" @click="save">
        提交
      </a-button>
    </template>
    <a-input-number
      v-model="value"
      :min="1"
      :formatter="value => `${value}${record.unit}`"
      :parser="value => value.replace(record.unit, '')" />
  </a-modal>
</template>

<script>
import { outSqare, inSqare } from '@/api/store/fastoutstore'

export default {
  data () {
    return {
      loading: false,
      visible: false,
      value: 1,
      record: {},
      type: null,
      title: ''
    }
  },
  methods: {
    base (record, type) {
      this.visible = true
      this.record = record
      this.type = type
      if (type === 1) {
        this.title = '出库'
        return
      }
      this.title = '入库'
    },
    save () {
      this.loading = true
      console.log(this.value)
      if (this.type === 1) {
        outSqare({
          spareId: this.record.id,
          outNum: this.value
        }).then(res => {
          this.$message.success('出库成功！')
          this.handleCancel()
        })
        return
      }
      inSqare({
        spareId: this.record.id,
        inNum: this.value
      }).then(res => {
        this.$message.success('入苦成功！')
        this.handleCancel()
      })
    },
    handleCancel (e) {
      this.visible = false
      this.loading = false
      this.$emit('ok')
    }
  }
}
</script>

<style>

</style>
